package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.entity.User;
import com.webserver.http.HttpServeletRequest;
import com.webserver.http.HttpServeletResponse;
import com.webserver.util.DBUtil;

import java.io.*;
import java.net.URISyntaxException;
import java.sql.*;

@Controller
public class UserController {
    private static File users = new File("users");
    private static File root;
    private static File staticDir;
    static {
        try {
            if (!users.exists()){
                users.mkdirs();
            }
            root = new File(UserController.class.getClassLoader().getResource(".").toURI());
            staticDir = new File(root, "static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/regUser")
    public void reg(HttpServeletRequest request, HttpServeletResponse response){
        System.out.println("开始处理用户注册");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String agestr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+agestr);
        if (username==null||username.isEmpty()||password==null||password.isEmpty()||
                nickname==null||nickname.isEmpty()||agestr==null||agestr.isEmpty()
        ){
            response.sendRedirect("/reg_infor_error.html");
            return;
        }
        int age = Integer.parseInt(agestr);
        try (Connection conn = DBUtil.getConnection();){
            String sql = "SELECT username "+
                         "FROM userinfo "+
                         "WHERE username =？;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("/reg_fail.html");
                return;
            }
           sql="INSERT INTO userinfo(username,password,nickname,age) " +
                "VALUES (?,?,?,?);";
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nickname);
            ps.setInt(4,age);
            int su = ps.executeUpdate(sql);
            if (su>0){
                System.out.println("注册成功");
                response.sendRedirect("/reg_success.html");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
    @RequestMapping("/login")
    public void login(HttpServeletRequest request,HttpServeletResponse response) throws IOException, ClassNotFoundException {
        String userName = request.getParameter("username");
        String password = request.getParameter("password");
        if (userName==null||userName.isEmpty()||password==null||password.isEmpty()){
            response.sendRedirect("/login_infor_error.html");
            return;
        }
        try ( Connection conn = DBUtil.getConnection();){
            String sql = "SELECT username,password FROM userinfo WHERE username=? AND password=?;";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,userName);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery(sql);
            if (rs.next()){
                response.sendRedirect("/login_success.html");
                return;
            }
            response.sendRedirect("/login_fail.html");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
}
